#include "signup.h"
#include "login.h"
#include "ui_signup.h"
#include "mainwindow.h"
#include "comlogin.h"
#include <QSqlDatabase> //数据驱动
#include <QSqlQuery> //数据库执行语句
#include <QMessageBox>
#include <QDebug>

SignUp::SignUp(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::SignUp)
{
    ui->setupUi(this);
    setWindowFlags(Qt::FramelessWindowHint);
    //设置图片
    QPixmap *pix = new QPixmap(":/images/tree.png");
    QSize sz = ui->label_image->size();
    ui->label_image->setPixmap(pix->scaled(sz));
    QPixmap *pix2 = new QPixmap(":/images/cuteIcon2.ico");
    QSize sz2 = ui->label_icon->size();
    ui->label_icon->setPixmap(pix2->scaled(sz2));
    QPixmap *pix3 = new QPixmap(":/images/tree4.png");
    QSize sz3 = ui->label_image2->size();
    ui->label_image2->setPixmap(pix3->scaled(sz3));
    //设置注册界面的背景图
    this->setFixedSize(800,540);//固定窗口大小
    QPalette pal =this->palette();
    QImage ImgAllbackground(":/images/background1.jpg");
    QImage fitimpic = ImgAllbackground.scaled(this->width(),this->height(),Qt::IgnoreAspectRatio);
    pal.setBrush(QPalette::Window,QBrush(fitimpic));
    setPalette(pal);
}

bool SignUp::mysql_init(){
    QSqlDatabase db;
        QString dsn;
        //先判断是否已经连接
        if(QSqlDatabase::contains("qt_sql_default_connection"))
          db = QSqlDatabase::database("qt_sql_default_connection");
        //没有则连接自己的数据库
        else{
          db = QSqlDatabase::addDatabase("QODBC");
          dsn = "Driver={MySQL ODBC 8.0 UniCode Driver};"; // Driver is now {MySQL}
//              dsn.append("Server=192.168.112.132,3306;"); // IP,Port
              dsn.append("Server=192.168.0.196,3306;"); // IP,Port
              dsn.append("Database=calculate24;"); // Schema
              dsn.append("Uid=root;"); // User
              dsn.append("Pwd=1234;"); // Password
              dsn.append("MULTI_HOST=1;");
              db.setDatabaseName(dsn);
        }
        if(!db.open())
        {
            qDebug()<<"open error";
            QMessageBox::information(this,"404","失败!绝对不是管理员的问题!");
            return false;
        }
        else{
            qDebug()<<"table create success";
            return true;
        }
}

//void SignUp::setLogin(Login l){
//    this->l = l;
//}

SignUp::~SignUp()
{
    delete ui;
}

void SignUp::on_closeBtn_clicked()
{
    close();
}

void SignUp::on_return_login_clicked()
{
    this->close();
    comLogin::p_login->exec();
}

void SignUp::on_sign_up_clicked()
{
    if(!mysql_init()){
        return;
    }
    else{
        QString username = ui->le_username->text();
        QString password = ui->le_password1->text();
        QString surepass = ui->le_password2->text();
        //判断密码是否一致
        if(password == surepass)
        {
            QString sql=QString("insert into tb_user(username,password) values('%1','%2');")
                        .arg(username).arg(password);
            //创建执行语句对象
            QSqlQuery query;
            //判断执行结果
            if(!query.exec(sql)){
                qDebug()<<"insert into error";
                QMessageBox::information(this,"注册认证","注册失败！");
            }
            else
            {
                qDebug()<<"insert into success";
                QMessageBox::information(this,"注册认证","注册成功！请登录");
                this->close();
    //            comLogin::p_login->exec();
            }

        }else{
            QMessageBox::information(this,"注册认证","两次密码输入不一致");
        }
    }
}
